package com.vf.cloud.paas.app.master.api.project.controller;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.api.project.service.IProjectClassify;
import com.vf.cloud.paas.app.master.common.domain.ProjectClassify;
import com.vf.cloud.paas.app.master.common.domain.User;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.log.annotation.RLog;
import com.vf.cloud.paas.common.security.annotation.TokenRequired;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import com.vf.cloud.paas.common.util.WebUtils;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/project/classify")
public class ProjectClassifyController {

	@Autowired
	private IProjectClassify projectClassifyImpl;
	
	@RLog(value = "获取底板组",type = "user")
	@TokenRequired
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<ProjectClassify>> list(@RequestBody ProjectClassify param) {
		return R.ok(projectClassifyImpl.getChildren(param));
	}
	

	@RLog(value = "异步加载底板组",type = "user")
	@TokenRequired
	@RequestMapping(value = "/lazyTree", method = RequestMethod.POST)
	public R<List<ProjectClassify>> tree(@RequestBody ProjectClassify param) {
		return R.ok(projectClassifyImpl.getChildrens(param));
	}
	
	@RequestMapping(value = "/selectTree", method = RequestMethod.POST)
	public R<List<ProjectClassify>> selectTree(@RequestBody ProjectClassify param) {
		List<ProjectClassify> rootList=new LinkedList<ProjectClassify>();
		ProjectClassify root=new ProjectClassify();
		root.setId("-1");
		root.setPid("root");
		root.setName("默认");
		root.setSort(0);
		root.setChildren(projectClassifyImpl.getChildren(param));
		rootList.add(root);
		return R.ok(rootList);
	}

	@RLog(value = "保存底板组",type = "user")
	@TokenRequired
	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<ProjectClassify> save(HttpServletRequest request, @RequestBody ProjectClassify projectClassify) {
		User user = WebUtils.getCurUser(request);
		if (projectClassify == null) {
			return R.failed("参数必填.");
		}
		if (StrKit.isBlank(projectClassify.getId())) {
			projectClassify.setId(UuidUtil.getUUID());
			projectClassify.setCreateUserId(user.getId());
			if (StrKit.isBlank(projectClassify.getPid()))
				projectClassify.setPid("-1");
			if (projectClassify.save())
				return R.ok();
		} else {
			projectClassify.setModifyUserId(user.getId());
			if (projectClassify.update())
				return R.ok();
		}
		return R.failed();
	}

	@RLog(value = "删除底板组",type = "user")
	@TokenRequired
	@Tx
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody ProjectClassify assetClassify) {
		try {
			if (assetClassify == null || StrKit.isBlank(assetClassify.getId())) {
				return R.failed("参数必填.");
			}
			projectClassifyImpl.delete(assetClassify.getId());
			return R.ok();
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

}
